import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
import { CommonService } from '../../../../services-common/common.service';
import { CommidityLabelManageService } from './commidity-label-manage.service';

@Component({
  selector: 'app-commidity-label-list',
  templateUrl: './commidity-label-list.component.html',
  styleUrls: ['./commidity-label-list.component.scss']
})
export class CommidityLabelListComponent implements OnInit {

  // 路由缓存恢复时
  static updateCache: any = null;

  // 适用场景
  useSceneOptions: any = [
    {value: '1', label: '列表'},
    {value: '2', label: '详情'},
    {value: '3', label: '购物车'},
    {value: '4', label: '结算页'}
  ];
  searchOptions = [
    { type: 1, labelText: '标题', valueKey: 'name', value: '' },
    {
      type: 2,
      labelText: '样式',
      valueKey: 'styleFlag',
      value: [],
      multiple: true,
      notTostr: true,
      selectBindKey: { key: 'content', label: 'name' },
      selectOptions: []
    },
    {
      type: 2,
      labelText: '类型',
      valueKey: 'type',
      value: [],
      multiple: true,
      notTostr: true,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [
        {value: '1', label: '自定义'},
        {value: '2', label: '优惠券'},
        {value: '3', label: '直降促销'}
      ]
    },
    {
      type: 2,
      labelText: '适用场景',
      valueKey: 'useScene',
      value: [],
      multiple: true,
      notTostr: true,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...this.useSceneOptions]
    },
    {
      type: 2,
      labelText: '适用类型',
      valueKey: 'userType',
      value: [],
      multiple: true,
      notTostr: true,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [
        {value: '0', label: '通用'},
        {value: '1', label: 'C端'},
        {value: '2', label: 'B端'}
      ]
    },
    {
      type: 2,
      labelText: '状态',
      valueKey: 'status',
      value: [],
      multiple: true,
      notTostr: true,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [
        {value: '1', label: '启用'},
        {value: '2', label: '禁用'}
      ]
    }
  ];
  searchParams = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  // 商品样式style options
  goodsStyleOptions: any = [];

  constructor(
    private message: NzMessageService,
    private router: Router,
    private commonService: CommonService,
    private services: CommidityLabelManageService
  ) { }

  ngOnInit(): void {
    CommidityLabelListComponent.updateCache = () => {
      this.loadGoodStyleData();
      this.loadTable();
    }
    this.loadGoodStyleData();
  }

  // 查询商品样式options
  async loadGoodStyleData() {
    this.goodsStyleOptions = await this.commonService.getDictList('goods_tag_styles');
    let tempIndex = this.searchOptions.findIndex((item: any) => item.valueKey === 'styleFlag');
    this.searchOptions[tempIndex].selectOptions = this.goodsStyleOptions;
  }

  // 类型Text Filter
  getGoodsStyleText(tag: any) {
    let tempObj: any = this.goodsStyleOptions.find((item: any) => item.content === tag);
    return tempObj?.name || '-';
  }

  // 适用场景Text Filter
  getUseSceneText(str: any) {
    let textArr: any[] = [];
    let arr = str.split(',');
    textArr = this.useSceneOptions.filter((item: any) => arr.includes(item.value)).map(((m: any) => m.label));
    return textArr.length ? textArr.join('、') : '-';
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getGoodsLabelList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      this.tableParams.tableData = data.records || [];
      this.tableParams.total = data.total || 0;
    } else {
      this.message.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  /**
   * Jump Page
   * @param type 0：新增 1：编辑 2：查看
   */
  jumpPage(type: number, item?: any) {
    let params: any = {
      type
    }
    if (item) {
      params.id = item.id;
    }

    this.router.navigate(['/used-store/commidity-label-edit'], {
      queryParams: params
    })
  }
}
